// pages/course/detail/detail.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    data: '',
    vidioUrl: '',
    baseUrl: '',
    courseId: 3,
    userId: 1,
    userName: '',
    isMoreMark: false,
    title: [{
        id: 1,
        value: '课程详情'
      },
      {
        id: 3,
        value: '名师介绍'
      }, {
        id: 4,
        value: '课堂交流'
      },
      {
        id: 5,
        value: '课程评价'
      }
    ],
    id: 1,
    m4List: [{
      id: 1,
      value: '最新发表'
    }, {
      id: 2,
      value: '最后回复'
    }, {
      id: 3,
      value: '回复数量'
    }, ],
    m4Id: 1,
    talkData: [],
    moreResponse: false,
    resId: null,
    resData: [],
    markData: [],
    isAddMark: false,
    form_data: '',
    grade:'',
    // 星级评价
    stars: [0, 1, 2, 3, 4],
    normalSrc: '/images/normal.png',
    selectedSrc: '/images/selected.png',

  },
  // 名师介绍：咨询师详情
  szxq() {
    wx.navigateTo({
      url: '/pages/sousuo/teacher1/teacher1',
    })
  },
  // 切换导航列表
  setStatus(e) {
    // console.log(e);
    // 初始化数据
    this.setData({
      id: e.target.id,
      m4Id: 1,
      moreResponse: '',
      // 交流回复
      moreResponse: false,
      resId: null,
      grade:''
    })

    // 获取课程详情
    if (this.data.id == 1) {
      this.detailData()
    }
    // 获取交流详情
    if (this.data.id == 4) {
      this.talkList()
    }
    // 获取评价数据
    if (this.data.id == 5) {
      this.selectMarkList()
    }
  },


  // 课程详情
  detailData() {
    let that = this
    console.log("课程id:" + this.data.courseId);
    console.log("userID:"+this.data.userId);
    wx.request({
      url: that.data.baseUrl + 'course/list',
      method: 'GET',
      data: {
        courseId: that.data.courseId
      },
      success(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.setData({
            detail: res.data.data.list[0]
          })
        }
      }
    })
  },
  // 课程详情：收藏
  courseCollect() {
    console.log("收藏课程");
    let that = this
    wx.request({
      method: 'Post',
      url: that.data.baseUrl + 'userCourse/insert',
      data: {
        courseId: that.data.courseId,
        userId: that.data.userId
      },
      success(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          // 收藏成功
          that.detailData()
        }
        wx.showToast({
          title: res.data.message,
        })
      }
    })
  },


  // 课堂交流：切换排列方式
  m4Status(e) {
    // console.log(e.target);
    let id = e.target.id
    this.setData({
      m4Id: id,
      // 交流回复
      moreResponse: false,
      resId: null
    })
    // 根据id查询数据排列
    this.talkList()
  },
  //  课堂交流：查看
  talkList() {
    let that = this
    let url = ''
    if (this.data.m4Id == 1) url = that.data.baseUrl + 'courseTalk/find1'
    if (this.data.m4Id == 2) url = that.data.baseUrl + 'courseTalk/find'
    if (this.data.m4Id == 3) url = that.data.baseUrl + 'courseTalk/findOrderByCount'
    wx.request({
      url: url,
      method: 'GET',
      data:{
        courseId:that.data.courseId
      },
      success(res) {
        console.log(res.data);
        that.setData({
          talkData: res.data.data
        })
      }

    })

  },
  // 课堂交流：添加
  addTalk(e) {
    console.log(e);
    let that = this
    let title = e.detail.value.title
    if(title == null || title == ''){
      wx.showToast({
        title: '数据为空',
        icon:'error'
      })
      console.log("title:"+title);
      return
    }
    wx.request({
      url: that.data.baseUrl + '/courseTalk/insert',
      method: "POST",
      data: {
        title: e.detail.value.title,
        courseId: that.data.courseId,
        userId: that.data.userId,
      },
      success(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.talkList()
          that.setData({
            form_data: ''
          })
        }
      }
    })
  },
  
  // 课堂交流：回复：查看状态
  moreResponse(e) {
    if (this.data.moreResponse) {
      this.setData({
        resId: null,
        moreResponse: false,
        resData: []
      })
    } else {
      let id = null
      if (e.target.id == this.data.resId) {
        this.setData({
          resId: null,
          moreResponse: false,
          resData: []
        })
      } else {
        this.setData({
          resId: e.target.id,
          moreResponse: true
        })
        this.replyData(e.target.id)
      }
    }
    console.log(this.data.resId);
  },
  // 课堂交流：回复：查看数据
  replyData(id) {
    let that = this
    // console.log(id);
    wx.request({
      url: that.data.baseUrl + 'courseReply/selectByTalkId?talkId=' + id,
      method: 'POST',
      // data:{
      //   talkId:id
      // },
      success(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.setData({
            resData: res.data.data
          })
        }
      }
    })
  },
  // 课堂交流：回复：添加
  addReply(e) {
    console.log(e);
    let that = this
    let replyText =e.detail.value.replyText 
    if(replyText == null || replyText == ''){
      wx.showToast({
        title: '数据为空',
        icon:'error'
      })
      console.log("replyText:"+replyText)
      return
    }
    wx.request({
      url: that.data.baseUrl + '/courseReply/insert',
      method: 'POST',
      data: {
        replyText: e.detail.value.replyText,
        talkId: that.data.resId,
        userId: that.data.userId,
      },
      success(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.replyData(that.data.resId)
          that.setData({
            form_data: ''
          })
        }
      }
    })
  },


  // 课堂评价：获取数据
  selectMarkList() {
    let that = this
    let pageSize = ''
    if (!this.data.isMoreMark) {
      pageSize = 5
    }
    wx.request({
      url: that.data.baseUrl + 'courseMark/list',
      method: 'GET',
      data: {
        pageNo: 1,
        pageSize: pageSize,
        courseId: that.data.courseId
      },
      success(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.setData({
            markData: res.data.data.list
          })
        }
        // console.log(that.data.markData);
      }
    })
  },
  // 课堂评价：查看更多评论
  viewMoreMark() {
    this.setData({
      isMoreMark: !this.data.isMoreMark
    })
    this.selectMarkList()
  },
  // 课程评价：添加回复的输入
  isAddMark() {
    this.setData({
      isAddMark: !this.data.isAddMark
    })
  },
  // 课程评价：添加回复
  addMark(e) {
    let that = this
    console.log(e.detail);
    let content = e.detail.value.content
    let grade = this.data.grade
    if (content == null || content == '' || grade == null || grade == '') {
      wx.showToast({
        title: '数据为空',
        icon:'error'
      })
      console.log("content:"+content+",grade="+grade);
      return;
    }
    wx.request({
      url: that.data.baseUrl + 'courseMark/add',
      method: "POST",
      data: {
        content: e.detail.value.content,
        grade: that.data.grade,
        userId: that.data.userId,
        courseId: that.data.courseId,
      },
      success(res) {
        that.selectMarkList()
        if (res.data.status == 200) {
          that.setData({
            isAddMark: false,
            grade:''
          })
          that.detailData()
        }
      }
    })

  },
  //课程评价：添加回复 ：点击选择整颗星评分
  selectRight: function (e) {
    var key = e.currentTarget.dataset.key
    // console.log("得" + key + "分")
    this.setData({
      grade: key
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options);
    // console.log(this.data.title);
    // courseId,userId
    // console.log(app);
    let userInfo = app.globalData.userInfo
    let globalData = app.globalData
    this.setData({
      baseUrl:globalData.URL,
      vidioUrl:globalData.vidioUrl
    })
    if(userInfo == null || userInfo == ''){
      wx.navigateTo({
        url: '/pages/my/login/login',
      })
    }else{
      this.setData({
        // userId:app.globalData.userInfo.id
        userId:userInfo.id,
      })
      console.log(options);
      if (options.courseId != undefined) {
        this.setData({
          courseId: options.courseId
        })
      }
      this.detailData()
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // console.log(this.data.id);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})